package com.egg.ylt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.adapter.ADA_TechnicianAdapter;
import com.egg.ylt.pojo.AppointmentServeListEntity;
import com.egg.ylt.pojo.AppointmentSubOrderEntity;
import com.egg.ylt.pojo.TechnicianEntity;
import com.egg.ylt.presenter.impl.ShopAppoinPresenterImpl;
import com.egg.ylt.view.IShopAppointmentView;
import com.egg.ylt.widget.GridViewForScrollView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.common.utils.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_ShopAppointment extends BaseActivity<ShopAppoinPresenterImpl> implements IShopAppointmentView {

    @BindView(R.id.layout_top)
    LinearLayout mLayoutTop;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.select_service_item_tv)
    TextView mSelectServiceItemNameTv;
    @BindView(R.id.time_appointment_tv)
    TextView mTimeAppointmentTv;
    @BindView(R.id.shop_selected_service_img_iv)
    ImageView mShopSelectedServiceImgIv;
    @BindView(R.id.shop_selected_service_name_tv)
    TextView mShopSelectedServiceNameTv;
    @BindView(R.id.shop_selected_service_description_tv)
    TextView mShopSelectedServiceDescriptionTv;
    @BindView(R.id.selected_service_item_rl)
    RelativeLayout mSelectedServiceItemRl;

    @BindView(R.id.technician_gv)
    GridViewForScrollView mTechnicianGv;

    @BindView(R.id.input_name_et)
    EditText mNameEt;
    @BindView(R.id.man_select_tv)
    TextView mManSelectTv;
    @BindView(R.id.woman_select_tv)
    TextView mWomenSelectTv;
    @BindView(R.id.phone_number_et)
    EditText mPhoneNumberEt;
    @BindView(R.id.remark_et)
    EditText mRemarkEt;

    private int INTENT_REQUEST_CODE = 0x000001;

    private String mSex = "";
    private String mTechnicianId;
    private String mTechnicianName;
    public static String sShopId;
    private List<TechnicianEntity.ListEntity> mTechnicianEntities;
    private ADA_TechnicianAdapter mTechnicianAdapter;

    private String mServiceName;
    private String mServiceIntro;
    private String mServiceImageUrl;
    private String mServicePrice;
    private String mOrderId;
    private String mServiceId;
    private String mStorePhone;

    @Override
    protected void getBundleExtras(Bundle extras) {
        mOrderId = extras.getString("orderId");
        sShopId = extras.getString("shopId");
        mStorePhone = extras.getString("storePhone");
        mServiceId = extras.getString("serviceId");
        mServicePrice = extras.getString("servicePrice");
        mServiceName = extras.getString("serviceName");
        mServiceIntro = extras.getString("serviceIntro");
        mServiceImageUrl = extras.getString("serviceImageUrl");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_shop_appointment;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mLayoutTop);

        //设置title
        includeTvTitle.setText("填写预约信息");

        //本地sp
        AppSharedPreferences sharedPreferences = new AppSharedPreferences(mContext);
        if (!StringUtil.isEmpty(mOrderId)) {
            mPresenter.getSubOrderById(sharedPreferences.getString(Constants.TOKEN), mOrderId);
        } else {
            //默认勾选先生
            mManSelectTv.setSelected(true);
            mSex = "先生";
            mPhoneNumberEt.setText(sharedPreferences.getString(Constants.USER_PHONE));
            //带入服务进来
            if (!StringUtil.isEmpty(mServiceName)) {
                mTimeAppointmentTv.setText("");
                mSelectServiceItemNameTv.setText(mServiceName);
                //展示选中的预约服务
                mSelectedServiceItemRl.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load(mServiceImageUrl)
                        .fallback(R.mipmap.ic_serve_default)
                        .error(R.mipmap.ic_serve_default)
                        .into(mShopSelectedServiceImgIv);
                mShopSelectedServiceNameTv.setText(mServiceName);
                mShopSelectedServiceDescriptionTv.setText(mServiceIntro);
            }
        }
        mPresenter.getTechnicianStatus(Constants.TOKEN, "", sShopId, "");
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @OnClick({R.id.include_iv_back, R.id.select_service_time_ll, R.id.man_select_tv, R.id.woman_select_tv,
            R.id.select_service_item_ll
            , R.id.order_now_tv})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.include_iv_back://返回键
                finish();
                break;
            case R.id.select_service_time_ll://选择时间
                //选中的服务项目
                if (StringUtil.isEmpty(mServiceId)) {
                    ToastUtil.makeText(mContext, "请先选择服务项目", false);
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString("serviceId", mServiceId);
                readyGoForResult(ACT_SelectTime.class, 1101, bundle);
                break;
            //选择先生
            case R.id.man_select_tv:
                mSex = "先生";
                mManSelectTv.setSelected(true);
                mWomenSelectTv.setSelected(false);
                break;
            //选择女士
            case R.id.woman_select_tv:
                mSex = "女士";
                mWomenSelectTv.setSelected(true);
                mManSelectTv.setSelected(false);
                break;
            //选择预约服务
            case R.id.select_service_item_ll:
                Bundle b = new Bundle();
                b.putString("shopId", sShopId);
                b.putString("storePhone", mStorePhone);
                Intent intent = new Intent(this, ACT_SelectServiceItem.class);
                intent.putExtras(b);
                startActivityForResult(intent, INTENT_REQUEST_CODE);
                break;
            //立即预约
            case R.id.order_now_tv:
                setPredetermine();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constants.SELECT_SERVICE_ITEM) {
            mServicePrice = data.getStringExtra("servicePrice");
            mServiceIntro = data.getStringExtra("serviceIntro");
            mServiceImageUrl = data.getStringExtra("serviceImageUrl");
            mServiceId = data.getStringExtra("serviceId");
            mServiceName = data.getStringExtra("serviceName");

            mTimeAppointmentTv.setText("");
            mSelectServiceItemNameTv.setText(mServiceName);
            //展示选中的预约服务
            mSelectedServiceItemRl.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .load(mServiceImageUrl)
                    .fallback(R.mipmap.ic_serve_default)
                    .error(R.mipmap.ic_serve_default)
                    .into(mShopSelectedServiceImgIv);
            mShopSelectedServiceNameTv.setText(mServiceName);
            mShopSelectedServiceDescriptionTv.setText(mServiceIntro);

        } else if (resultCode == Constants.SELECT_SERVICE_TIME) {
            mTimeAppointmentTv.setText(data.getStringExtra("time"));
            mPresenter.getTechnicianStatus(Constants.TOKEN, data.getStringExtra("time") + ":00", sShopId, mServiceId);
        }
    }

    @Override
    public void setAppointmentSucceed(AppointmentServeListEntity.ListEntity entity) {
        //预约完后去成功界面，关闭当前页面
        Bundle bundle = new Bundle();
        bundle.putSerializable("entity", entity);
        readyGo(ACT_AppointmentSuccess.class, bundle);
        finish();
    }

    @Override
    public void setTechnician(List<TechnicianEntity.ListEntity> technicianEntities) {

        mTechnicianEntities = technicianEntities;

        if (mTechnicianAdapter == null) {
            mTechnicianAdapter = new ADA_TechnicianAdapter();
            mTechnicianGv.setAdapter(mTechnicianAdapter);
            mTechnicianAdapter.setData(technicianEntities);
        } else {
            mTechnicianAdapter.setData(technicianEntities);
        }

        mTechnicianAdapter.setOnTechnicianCheckedListener(new ADA_TechnicianAdapter.OnTechnicianCheckedListener() {
            @Override
            public void onTechnicianChecked(String technicianId, String technicianName) {
                mTechnicianId = technicianId;
                mTechnicianName = technicianName;
            }
        });
    }

    @Override
    public void setAppointmentSubOrderData(AppointmentSubOrderEntity entity) {
        mSelectedServiceItemRl.setVisibility(View.VISIBLE);
        mSelectServiceItemNameTv.setText(entity.getService_name());
        mShopSelectedServiceNameTv.setText(entity.getService_name());
        Glide.with(mContext)
                .load(entity.getImageUrl())
                .fallback(R.mipmap.ic_serve_default)
                .error(R.mipmap.ic_serve_default)
                .into(mShopSelectedServiceImgIv);
        mShopSelectedServiceDescriptionTv.setText("时长" + entity.getServiceTime() + "分钟");
        mNameEt.setText(entity.getUser_name());
        mPhoneNumberEt.setText(entity.getUser_phone());
        mRemarkEt.setText(entity.getRemark());
        mSex = entity.getGender();
        if (mSex.equals("先生")) {
            mManSelectTv.setSelected(true);
            mWomenSelectTv.setSelected(false);
        } else {
            mWomenSelectTv.setSelected(true);
            mManSelectTv.setSelected(false);
        }
        mServicePrice = entity.getPrice();
        mServiceId = entity.getService_id();
        mServiceName = entity.getService_name();
    }

    /**
     * 把用户预约数据传给后台
     */
    public void setPredetermine() {
        //选中的服务项目
        if (StringUtil.isEmpty(mServiceId)) {
            ToastUtil.makeText(mContext, "请选择服务项目", false);
            return;
        }

        //到店时间
        if (TextUtils.isEmpty(mTimeAppointmentTv.getText().toString())) {
            ToastUtil.makeText(mContext, "请选择到店时间", false);
            return;
        }

        //判断姓名
        if (TextUtils.isEmpty(mNameEt.getText().toString())) {
            ToastUtil.makeText(mContext, "请输入您的姓名", false);
            return;
        }

        //判断性别是否选择
        if (TextUtils.isEmpty(mSex)) {
            ToastUtil.makeText(mContext, "请选择性别", false);
            return;
        }

        //判断手机号是否正确
        if (!StringUtil.isPhone(mPhoneNumberEt.getText().toString().trim())) {
            ToastUtil.makeText(mContext, "手机号不正确", false);
            return;
        }

        //没选技师默认选择一个
        if (StringUtil.isEmpty(mTechnicianId)) {
            for (int i = 0; i < (mTechnicianEntities == null ? 0 : mTechnicianEntities.size()); i++) {
                if (mTechnicianEntities.get(i).getTechnicianStatus().equals("0")) {
                    mTechnicianId = mTechnicianEntities.get(i).getTechnicianId();
                    mTechnicianName = mTechnicianEntities.get(i).getTechnicianName();
                    break;
                }
            }
        }

        if (StringUtil.isEmpty(mTechnicianId)) {
            ToastUtil.makeText(mContext, "该时间段无空闲技师，请重新选择时间段", false);
            return;
        }

        //上传预约服务数据
        mPresenter.setAppoinInfo(Constants.TOKEN, Constants.COMPANYID, mNameEt.getText().toString(),
                mPhoneNumberEt.getText().toString().trim(), mRemarkEt.getText().toString(),
                mTimeAppointmentTv.getText().toString(), mSex, sShopId, mServiceId,
                mServiceName, mServicePrice,
                mTechnicianId,
                mTechnicianName);
    }

}
